package net.lastcoder.ifserver.util;

import net.lastcoder.ifserver.Constants;
import net.lastcoder.ifserver.Globals;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

/**
 * date: 2016/8/17
 */
public class FileUtil {
    private static final Logger Logger = LoggerFactory.getLogger(FileUtil.class);


    /**写数据到文件
     * @param is 请自己关闭
     * @param file
     * @throws IOException
     */
    public static void writeData2File(InputStream is, File file) {
        FileOutputStream os = null;
        try {
            byte[] buffer = new byte[is.available()];
            is.read(buffer);

            file.createNewFile();
            os = new FileOutputStream(file);
            os.write(buffer);
            os.flush();
            os.close();
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(os);
        }
    }

    public static void writeData2File(byte[] data, File file) {
        FileOutputStream os = null;
        try {
            file.createNewFile();
            os = new FileOutputStream(file);
            os.write(data);
            os.flush();
            os.close();
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(os);
        }
    }

    /**
     * @param data
     * @param filename  是相对目录，保存到files文件夹
     */
    public static void writeData2File(byte[] data, String filename) {
        FileOutputStream os = null;
        try {
            File file = new File(Globals.getFilesDir(), filename);
            file.createNewFile();
            os = new FileOutputStream(file);
            os.write(data);
            os.flush();
            os.close();
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(os);
        }
    }

    public static byte[] readFileData(String filename) {
        FileInputStream is = null;
        File file = new File(filename);
        try {
            is = new FileInputStream(file);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);

            return buffer;
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(is);
        }

        return null;
    }

    public static byte[] readFileData(File file) {
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);

            return buffer;
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(is);
        }

        return null;
    }


    public static String getFileContent(String filePath)  {
        File file = new File(filePath);
        BufferedInputStream bis = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            byte[] data = new byte[bis.available()];
            bis.read(data);
            return new String(data, Constants.DEFAUT_CAHRSET);
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(bis);
        }

        return null;
    }

    public static String getFileContent(File file)  {
        BufferedInputStream bis = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            byte[] data = new byte[bis.available()];
            bis.read(data);
            return new String(data, Constants.DEFAUT_CAHRSET);
        } catch (IOException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(bis);
        }

        return null;
    }


    public static boolean appendFile(String filename, String content) throws IOException {
        return appendFile(new File(filename), content);
    }

    public static boolean appendFile(File file, String content) throws IOException {
        if (TextUtils.isEmpty(content)) {
            return true;
        }

        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            fileWriter = new FileWriter(file, true);
            bufferedWriter = new BufferedWriter(fileWriter);
            if (PlatformUtil.isWindows()) {
                content = content + "\r\n";
            } else {
                content = content + "\n";
            }
            bufferedWriter.write(content);
            bufferedWriter.flush();
        }
        finally {
            IOUtil.closeSilently(bufferedWriter);
        }

        return true;
    }

    public static boolean copy(String src, String dest) {
        return copy(new File(src), new File(dest));
    }

    public static boolean copy(File src, String dest) {
        return copy(src, new File(dest));
    }

    public static boolean copy(File src, File dest) {
        if (!src.exists()) {
            Logger.error("src file is NOT EXIST.");
            return false;
        }

        if (dest.exists()) {
            Logger.error("dest file is EXIST.");
            return false;
        }


        OutputStream os = null;
        try {
            if (!dest.createNewFile()) {
                return false;
            }
            os = new FileOutputStream(dest);
            Files.copy(src.toPath(), os);

        } catch (IOException | SecurityException e) {
            Logger.error(e.getMessage(), e);
        } finally {
            IOUtil.closeSilently(os);
        }

        return true;
    }
}
